package cn.bdqn.witcity.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.ObjectPostProcessor;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.access.intercept.FilterSecurityInterceptor;

@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Bean
    PasswordEncoder passwordEncoder(){
        return NoOpPasswordEncoder.getInstance();
    }
    public void configure(HttpSecurity http) throws Exception{
        http.headers().frameOptions().disable();
        http.authorizeRequests().withObjectPostProcessor(
                new ObjectPostProcessor<FilterSecurityInterceptor>() {

                    @Override
                    public <O extends FilterSecurityInterceptor> O postProcess(O o) {
                        o.setSecurityMetadataSource(cfism());
                        o.setAccessDecisionManager(cadm());
                        return o;
                    }
                }).and().formLogin().successForwardUrl("/index")
                .permitAll().and().csrf().disable();

    }
    @Bean
    CustomFilterInvocation cfism(){
        return new CustomFilterInvocation();
    }
    @Bean
    CustomAccessManager cadm(){
        return new CustomAccessManager();
    }

}
